
import gameConfig from "../../config/gameConfig";
import UIService from "../../services/uiService";
import UserService from "../../services/userService";
import { mgrs, sers } from "../../system/OverallSystem ";
import PageBase from "./pageBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class InfinityPowerPage extends PageBase {

    public pageName: string = "无限体力页面";

    private static infinityLookCnt: number = 0;

    @property({ type: cc.Node, displayName: "确认按钮" })
    private confirmBtn: cc.Node = null;

    confirmCB: () => void = () => { };

    protected colseDoFun2: Function = null;

    setCloseDoFun2(colseDoFun: Function) {
        this.colseDoFun2 = colseDoFun;
    }

    onLoad() {

    }

    start() {
        this.init();
    }

    protected init(): void {

        this.updateVideoCnt();
        this.open();
    }

    OnBtnGetFWPower() {
        mgrs().soundMgr.playBtnSound();

        sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
            sers().userSrv.addPower(5);
            sers().uiSrv.showMessage("获得体力5点");
        }, (err) => {

        }, null, "无限体力页面领取五点体力");
    }

    OnBtnEnterInifinityMod() {
        mgrs().soundMgr.playBtnSound();
        let isInfinityPowerModel = Number(sers().localStorageSrv.get(UserService.infinityPowerModel)) != 0;
        if (isInfinityPowerModel) {
            //XXX 
            sers().uiSrv.showMessage("已经开启无限体力模式！");
            this.close();
        } else {
            sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
                InfinityPowerPage.infinityLookCnt++;
                this.updateVideoCnt();
                if (InfinityPowerPage.infinityLookCnt >= gameConfig.INFINITY_POWER_VIDEO_CNT) {
                    sers().userSrv.addPower(999);
                    sers().localStorageSrv.set(UserService.infinityPowerModel, 1);
                    sers().localStorageSrv.set(UserService.infinityPowerDate, Date.now());
                    this.close();
                }
            }, () => {

            }, null, "无限体力");
        }
    }

    private updateVideoCnt() {
        if (gameConfig.INFINITY_POWER_VIDEO_CNT > 1) {
            const cnt: cc.Label = this.confirmBtn.getChildByName("cnt").getComponent(cc.Label);
            cnt.string = `${InfinityPowerPage.infinityLookCnt}/${gameConfig.INFINITY_POWER_VIDEO_CNT}`;
        } else {
            this.confirmBtn.getChildByName("cnt").active = false;
        }
    }

    close() {
        this.closeAni()
            .then(() => {
                sers().uiSrv.deletePageRecord(this.id);
                sers().uiSrv.showReceiveInfinityPage(this.colseDoFun2);
                this.node.destroy();
            })
            .catch();
    }

    OnBtnClose(): void {
        mgrs().soundMgr.playBtnSound();
        this.closeAni().then(() => {
            this.colseDoFun && this.colseDoFun(true);
            sers().uiSrv.deletePageRecord(this.id);
            this.node.destroy();
        });
    }

    update(dt) {

    }
}
